package algorithm.string.baisc;

public class BruteForce02 {
    /**
     * 蛮力匹配2
     */
    public static int indexOf(String text, String pattern) {
        if (text == null || pattern == null) return -1;
        char[] textChars = text.toCharArray();
        int tLen = textChars.length;
        char[] patternChars = pattern.toCharArray();
        int pLen = patternChars.length;
        if (tLen == 0 || pLen == 0 || tLen < pLen) return -1;
        int tiMax = tLen - pLen;
        for (int ti = 0; ti <= tiMax; ti++) {
            int pi = 0;
            for (; pi < pLen; pi++) {
                if (textChars[ti + pi] != patternChars[pi]) break;
            }
            if (pi == pLen) return ti;
        }
        return -1;
    }
}
